package com.aldobucchi.core.comparison.simple
{
	
	import com.aldobucchi.core.comparison.result.ComparisonResult;
	import com.aldobucchi.core.comparison.result.ComparisonResults;
	import com.aldobucchi.core.comparison.ComparisonUtil;
		
	/**
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */	
	public class FieldSimpleComparator implements ISimpleComparator
	{
		
		public var fieldName:String;
		
		public var comparator:ISimpleComparator;
		
		
		public function FieldSimpleComparator( fieldName:String = null, comparator:ISimpleComparator = null )
		{
			this.fieldName = fieldName;
			this.comparator = comparator;
		}
		

		
		public function compare( value1:*, value2:* ):ComparisonResult
		{
			
			if ( fieldName == null ) return ComparisonResults.DIFFERENT;
			
			if ( ! value1 is Object ) return ComparisonResults.DIFFERENT;
			if ( ! value2 is Object ) return ComparisonResults.DIFFERENT;			
			
			var obj1:Object = value1 as Object;
			var obj2:Object = value1 as Object;
			
			if ( ! obj1.hasOwnProperty( fieldName ) ) return ComparisonResults.DIFFERENT;
			if ( ! obj2.hasOwnProperty( fieldName ) ) return ComparisonResults.DIFFERENT;
			
			return ComparisonUtil.compare( obj1[ fieldName ], obj2[ fieldName ], comparator );
		}
		
	}
	
}